/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml.Serialization;
using HtmlAgilityPack;

namespace LucasCode
{
    [Serializable]
    public class MatchPlayer : IComparable
    {
        public override bool Equals(object obj)
        {
            return (CompareTo(obj) == 0);
        }
        public int CompareTo(object b)
        {
            MatchPlayer p = (MatchPlayer)b, player = this;
            if ((!string.IsNullOrEmpty(player.UserID) && p.UserID == player.UserID) || (!string.IsNullOrEmpty(player.GameID) && p.GameID == player.GameID) || (p.GameAccountList.Contains(player.GameID) || player.GameAccountList.Contains(p.GameID)))
            {
                return 0;
            }
            return -1;
        }
        private string m_GameID = string.Empty;
        private StringCollection m_Names = new StringCollection();
        private string m_UserID = string.Empty;
        private string m_ESLNick = string.Empty;
        private string m_CurrentNick = string.Empty;
        private bool m_IsArchiveOwner = false;
        private bool m_IsBarred = false;
        private bool m_HasAequitasLog = false;

        public bool HasAequitasLog
        {
            get { return m_HasAequitasLog; }
            set { m_HasAequitasLog = value; }
        }
        private List<MatchAction> m_Actions = new List<MatchAction>();
        private MatchContestant m_Team = null;

        [XmlIgnore]
        public MatchContestant Team
        {
            get { return m_Team; }
            set { m_Team = value; }
        }

        public MatchPlayer()
        {
            
        }

        public MatchPlayer(string name)
        {
            this.m_CurrentNick = name;
            m_Names.Add(name);
        }

        public void lookupPlayer()
        {
            if (!string.IsNullOrEmpty(m_ESLNick) && !string.IsNullOrEmpty(m_UserID))
                return;
            MatchPlayer p = lookupPlayer(m_GameID);
            if (p != null)
            {
                this.m_ESLNick = p.m_ESLNick;
                this.m_UserID = p.m_UserID;
            }
        }

        public static MatchPlayer lookupPlayer(string GameID)
        {
            if (string.IsNullOrEmpty(GameID))
                return null;
            HtmlAgilityPack.HtmlDocument d = new HtmlAgilityPack.HtmlDocument();
            d.LoadHtml(MatchHelpers.GetHTML(@"http://www.esl.eu/search/?query=" + GameID));

            HtmlNode result = d.DocumentNode.SelectSingleNode(@"//div[@id=""main_content""]/div/div/div[@class=""searchresult""]/a[@href]");
            if (result != null)
            {
                MatchPlayer p = new MatchPlayer(result.InnerText);
                p.ESLNick = result.InnerText;
                p.GameID = GameID;
                p.UserID = MatchHelpers.getIDFromLink(result.Attributes["href"].Value);
                return p;
            }
            return null;
        }

        [XmlIgnore]
        public int Kills
        {
            get
            {
                int kills = 0;
                foreach (MatchAction a in m_Actions)
                {
                    if (a.Action == MatchActions.Kills)
                        kills++;
                }
                return kills;
            }
        }

        [XmlIgnore]
        public int Headshots
        {
            get 
            {
                int headshots = 0;
                foreach (MatchAction a in m_Actions)
                {
                    if (a.Action == MatchActions.Kills && (a.Flags & MatchActionFlags.Headshot) != 0)
                        headshots++;
                }
                return headshots;
            }
        }

        [XmlIgnore]
        public int Deaths
        {
            get
            {
                int deaths = 0;
                foreach (MatchAction a in m_Actions)
                {
                    if (a.Action == MatchActions.Dies)
                        deaths++;
                }
                return deaths;
            }
        }

        public string CurrentNick
        {
            get
            {
                return m_CurrentNick;
            }
            set
            {
                m_CurrentNick = value;
                if (!m_Names.Contains(value))
                    m_Names.Add(value);
            }
        }

        public string GameID
        {
            get
            {
                return m_GameID;
            }
            set
            {
                m_GameID = value;
            }
        }

        public string[] Names
        {
            get
            {
                string[] names = new string[m_Names.Count];
                m_Names.CopyTo(names, 0);
                return names;
            }
            set
            {
                m_Names.Clear();
                m_Names.AddRange(value);
            }
        }

        public string UserID
        {
            get { return m_UserID; }
            set { m_UserID = value; }
        }

        public string ESLNick
        {
            get { return m_ESLNick; }
            set { m_ESLNick = value; }
        }

        public MatchAction[] Actions
        {
            get
            {
                return m_Actions.ToArray();
            }
            set
            {
                m_Actions.Clear();
                m_Actions.AddRange(value);
            }
        }

        public bool IsArchiveOwner
        {
            get
            {
                return m_IsArchiveOwner;
            }
            set
            {
                m_IsArchiveOwner = value;
            }
        }

        public bool IsBarred
        {
            get
            {
                return m_IsBarred;
            }
            set
            {
                m_IsBarred = value;
            }
        }

        public int AddNick(string nick)
        {
            return m_Names.Add(nick);
        }

        public void AddNicks(string[] nicks)
        {
            m_Names.AddRange(nicks);
        }

        public void AddAction(MatchAction action)
        {
            m_Actions.Add(action);
        }

        public void AddActions(MatchAction[] actions)
        {
            m_Actions.AddRange(actions);
        }

        private GameAccountCollection m_GameAccounts = new GameAccountCollection();


        public GameAccount[] GameAccounts
        {
            get
            {
                return m_GameAccounts.ToArray();
            }
            set
            {
                m_GameAccounts.Clear();
                m_GameAccounts.Add(value);
            }
        }

        [XmlIgnore]
        public GameAccountCollection GameAccountList
        {
            get
            {
                return m_GameAccounts;
            }
        }
    }
}
